<!--
@name:dialogThemePoint
@author:Zhenglei
@date:10:32
@description:dialogThemePoint
@update:2022/8/2610:32
-->
<template>
  <el-dialog title="关联指征"
             :visible.sync="visible"
             :before-close="modalClose"
             :append-to-body="true"
             :close-on-click-modal="false"
             :close-on-press-escape="false"
             width="50%">
    <el-form :model="subData"
             label-position="right"
             label-width="150px"
             ref="formModelRef"
             :rules='formModelRules'>
      <el-form-item label="类型：" class="is-required">
        <el-select v-model="subData.indicationType"
                   class="w-300"
                   :disabled="isEdit"
                   @change="handleChangeType"
                   placeholder="请选择类型">
          <el-option v-for="item in typeDataSource"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="指征：" prop="indicationId">
        <el-select v-model="subData.indicationId"
                   class="w-300"
                   placeholder="请选择指征">
          <el-option v-for="item in pointerDataSource"
                     :key="item.id"
                     :label="item.indicationName"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="排序：" prop="sort">
        <el-input class="w-300" type="number" v-model="subData.sort" placeholder="请输入排序" clearable/>
      </el-form-item>
    </el-form>
    <p align="center">
      <el-button @click="modalClose"
                 type="info">取 消
      </el-button>
      <el-button type="primary"
                 v-prevent-re-click
                 @click="handleSave()">保 存
      </el-button>
    </p>

  </el-dialog>

</template>

<script>
export default {
  name: "dialogThemePoint",
  props: {
    pointDataObj: {
      type: Object,
      default: {}
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      subData: {
        indicationType: 4,
        indicationId: '',
        indicationName: '',
        sort: '',
      },
      isEdit: false,
      typeDataSource: [
        // {
        //   id: 1,
        //   name: '手动输入'
        // },
        {
          id: 4,
          name: '手动输入'
        },
        {
          id: 2,
          name: '量表'
        },
        {
          id: 3,
          name: '检测项'
        },

      ],

      pointerDataSource: [],
      formModelRules: {
        indicationId: [{required: true, message: '请选择指征', trigger: 'change'},],
        sort: [{required: true, message: '请输入排序', trigger: 'blur'},],
      }
    }
  },
  created() {
    if (this.pointDataObj.indicationType) {
      this.isEdit = true
      this.subData = Object.assign(this.subData, this.pointDataObj)
      this.getIndicationList(this.pointDataObj.indicationType)
    } else {
      this.isEdit = false
      this.getIndicationList(4)
    }
  },
  methods: {
    modalClose() {
      this.$emit('update:visible', false);
    },
    handleChangeType(val) {
      this.subData.indicationId = ''
      this.getIndicationList(val)
    },
    handleSave() {
      this.$refs.formModelRef.validate(valid => {
        if (valid) {
          this.subData.indicationName = this.pointerDataSource.find(item => item.id === this.subData.indicationId).indicationName
          this.$emit('handlePointResultData', this.subData)
          this.modalClose()
        }
      })
    },
    //查询指征
    getIndicationList(type) {
      this.post(this.api.healthMap.getIndicationList, {
        indicationType: type,
        pageSize: 500
      }).then((res) => {
        if (res.data.code === '200') {
          this.pointerDataSource = res.data.object
        }
      })
    },
  },
}
</script>

<style scoped>

</style>
